//
//  YTNewFlashCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTNewFlashCell.h"
@interface YTNewFlashCell ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;

@property (weak, nonatomic) IBOutlet UIButton *goodButton;
@property (weak, nonatomic) IBOutlet UIView *shareContainerView;
@property (weak, nonatomic) IBOutlet UIButton *badButton;


@property(nonatomic ,strong)UIButton *currentBtn; // 记录选中按钮
@end

@implementation YTNewFlashCell
- (IBAction)shareButtonClick:(UIButton *)sender
{
//    self.shareButton.selected = !sender.selected;
//
    self.changeLayout(self.tag);
    
    
}


- (void)setModel:(YTLivesModel *)model
{
    _model = model;
    
    self.timeLabel.text = model.created_at;
    self.titleLabel.text = model.post_title;
    self.detailLabel.attributedText = model.attrStr;
    
    // 利好
    if (model.is_like.boolValue) {
        
        self.goodButton.selected = YES;
        self.currentBtn = self.goodButton;
       [self.goodButton setTitle:[NSString stringWithFormat:@"利好 %@",model.up_counts] forState:UIControlStateSelected];
    }else{
        self.goodButton.selected = NO;
        [self.goodButton setTitle:[NSString stringWithFormat:@"利好 %@",model.up_counts] forState:UIControlStateNormal];
    }
    
    // 利空
    if (model.no_like.boolValue) {
        
        self.badButton.selected = YES;
        self.currentBtn = self.badButton;
        [self.badButton setTitle:[NSString stringWithFormat:@"利空 %@",model.down_counts] forState:UIControlStateSelected];
    }else{
        self.badButton.selected = NO;
        [self.badButton setTitle:[NSString stringWithFormat:@"利空 %@",model.down_counts] forState:UIControlStateNormal];
    }
}


- (IBAction)goodOrBadBtnClick:(UIButton *)sender
{
    // 判断是否有登陆
    if (!YTNetworkManager.shareManager.account.auth) {
        NSAssert(self.shouldLogin != nil, @"shouldLogin 不能为 nil");
        
        self.shouldLogin();
        return;
    }
    
    
    NSString *type = (sender.tag == 101) ? @"1" : @"0";
    
    [YTNetworkManager.shareManager postNewFlashGoodOrBadWithlid:self.model.ID
                                                           type:type
                                                     completion:^(BOOL isSucc, NSString *statusInfo)
    {
        if (!isSucc) {
            [self.superview showTipWithTipStr:statusInfo];
            return ;
        }

//        [type isEqualToString:@"1"] ? (statusInfo ? [self showAwardWithTipStr:@"添加利好 +%@EATI",statusInfo] : [self showAwardWithTipStr:@"添加利好"]) : (statusInfo ? [self showAwardWithTipStr:@"添加利好 +%@EATI",statusInfo] : [self showAwardWithTipStr:@"添加利好"]);
//        [self.superview showTipWithTipStr:statusInfo];
        
   
        int upCount = self.model.up_counts.intValue;
        int downCount = self.model.down_counts.intValue;
        // 利好
        if ([type isEqualToString:@"1"]) {
            statusInfo ? [self showAwardWithTipStr:[NSString stringWithFormat:@"投票成功 +%@算力",statusInfo]] : [self showAwardWithTipStr:@"投票成功"];
            [sender setTitle:[NSString stringWithFormat:@"利好 %d",upCount + 1] forState:UIControlStateSelected];
            self.model.up_counts = [NSString stringWithFormat:@"%d",upCount + 1];
            
            if (self.model.down_counts.intValue != 0) {
                self.model.down_counts = [NSString stringWithFormat:@"%d",downCount - 1];
                [self.currentBtn setTitle:[NSString stringWithFormat:@"利空 %d",downCount - 1] forState:UIControlStateNormal];

            }

            
        }else{
             statusInfo ? [self showAwardWithTipStr:[NSString stringWithFormat:@"投票成功 +%@算力",statusInfo]] : [self showAwardWithTipStr:@"投票成功"];
            [sender setTitle:[NSString stringWithFormat:@"利空 %d",downCount + 1] forState:UIControlStateSelected];
            self.model.down_counts = [NSString stringWithFormat:@"%d",downCount + 1];
            if (self.model.up_counts.intValue != 0) {
                [self.currentBtn setTitle:[NSString stringWithFormat:@"利好 %d",upCount - 1] forState:UIControlStateNormal];
                self.model.up_counts = [NSString stringWithFormat:@"%d",upCount - 1];
                
            }
        }
        
        self.currentBtn.selected = NO;
        sender.selected = YES;
        self.currentBtn = sender;
        
        if ([type isEqualToString:@"1"]) {
            self.model.is_like = @"1";
            self.model.no_like = @"0";
        }else{
            self.model.no_like = @"1";
            self.model.is_like = @"0";
        }
        
       
    }];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.badButton setTitle:@"选中利空" forState:UIControlStateSelected];
    [self.goodButton setTitle:@"选中利好" forState:UIControlStateSelected];
    
}



@end
